import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["radioType", "renderFormItem", "mode", "render"];
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import { ConfigProvider, Radio, Spin } from 'antd-v4';
import classNames from 'classnames';
import React, { useContext, useImperativeHandle, useRef } from 'react';
import { ObjToMap, proFieldParsingText, useFieldFetchData } from '../Select';
import './index.less';
/**
 * 单选组件
 *
 * @param param0
 * @param ref
 */

var FieldRadio = function FieldRadio(_ref, ref) {
  var radioType = _ref.radioType,
      renderFormItem = _ref.renderFormItem,
      mode = _ref.mode,
      render = _ref.render,
      rest = _objectWithoutProperties(_ref, _excluded);

  var _useContext = useContext(ConfigProvider.ConfigContext),
      getPrefixCls = _useContext.getPrefixCls;

  var layoutClassName = getPrefixCls('pro-field-radio');

  var _useFieldFetchData = useFieldFetchData(rest),
      _useFieldFetchData2 = _slicedToArray(_useFieldFetchData, 3),
      loading = _useFieldFetchData2[0],
      options = _useFieldFetchData2[1],
      _fetchData = _useFieldFetchData2[2];

  var radioRef = useRef();
  useImperativeHandle(ref, function () {
    return _objectSpread(_objectSpread({}, radioRef.current || {}), {}, {
      fetchData: function fetchData() {
        return _fetchData();
      }
    });
  });

  if (loading) {
    return _jsx(Spin, {
      size: "small"
    });
  }

  if (mode === 'read') {
    var optionsValueEnum = (options === null || options === void 0 ? void 0 : options.length) ? options === null || options === void 0 ? void 0 : options.reduce(function (pre, cur) {
      var _cur$value;

      return _objectSpread(_objectSpread({}, pre), {}, _defineProperty({}, (_cur$value = cur.value) !== null && _cur$value !== void 0 ? _cur$value : '', cur.label));
    }, {}) : undefined;

    var dom = _jsx(_Fragment, {
      children: proFieldParsingText(rest.text, ObjToMap(rest.valueEnum || optionsValueEnum))
    });

    if (render) {
      return render(rest.text, _objectSpread({
        mode: mode
      }, rest.fieldProps), dom) || null;
    }

    return dom;
  }

  if (mode === 'edit') {
    var _rest$fieldProps;

    var RadioComponents = radioType === 'button' ? Radio.Button : Radio;

    var _dom = _jsx(Radio.Group, _objectSpread(_objectSpread({
      ref: radioRef
    }, rest.fieldProps), {}, {
      className: classNames((_rest$fieldProps = rest.fieldProps) === null || _rest$fieldProps === void 0 ? void 0 : _rest$fieldProps.className, "".concat(layoutClassName, "-").concat(rest.fieldProps.layout || 'horizontal')),
      options: undefined,
      children: options === null || options === void 0 ? void 0 : options.map(function (item) {
        return _jsx(RadioComponents, _objectSpread(_objectSpread({}, item), {}, {
          children: item.label
        }), item.value);
      })
    }));

    if (renderFormItem) {
      return renderFormItem(rest.text, _objectSpread({
        mode: mode
      }, rest.fieldProps), _dom) || null;
    }

    return _dom;
  }

  return null;
};

export default /*#__PURE__*/React.forwardRef(FieldRadio);